package com.qf.servlet;

import cn.dsna.util.images.ValidateCode;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author AzureSky_X
 * @Date 2021/1/6 20:02
 * @Version 1.0
 * 验证码管理
 */
@WebServlet("/mycode")
public class MyCodeServlet extends HttpServlet {

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //ValidateCode(宽度,高度,输入字符数量,干扰线)
        ValidateCode validateCode = new ValidateCode(100,30,4,10);
        //生成验证码，赋值给ValidateCode的code属性
        validateCode.createCode();
        //控制台打印下验证码
        String code = validateCode.getCode();
        System.out.println(code);

        //把验证码存入session
        request.getSession().setAttribute("sessionCode",validateCode.getCode());
        //把验证码以流的形式写给页面
        validateCode.write(response.getOutputStream());
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}